$(document).ready(function(){
    var overlay = $("<div id='overlay' class='clearfix'><div id='panel'></div></div>'");
    overlay.appendTo(document.body);    
    $('#message').hide();    
    $('.bt-detail').click( //Tao mot Overlay Div de load noi dung chi tiet
        function(e){
            $.get($(this).attr('href'), function(data, status){
                if(status == 'success') //Kiem tra Ajax thanh cong se load noi dung tren overlay
                {
                    $(overlay).html(data);
                    $(overlay).show();
                    $('#bt-close').click(function(e) //Go bo Overlay DIV khi click vao nut close
                    {
                        $(overlay).fadeOut(800);
                    });
                }
                else
                    {
                        alert('Some error has been occured!');
                    }
            });
            return false;
        }
    );
    $('.bt-active').click(function(e) //Cai dat su kien khi click vao nut active
    {
        var target = $(e.target);
        var t = (target.text().trim()=='0')?'1':'0';
        $.get($(this).attr('href') + "&status=" + t, function(data, status){
            if(status == 'success' && data != 'FAIL')
                {
                    target.text(t);
                    target.attr('class', 'img-circle ' + ((t=='1')?'green':'red'));
                }
                else
                {
                    alert(status + " : " + data);
                }
        });
        return false;
    });
    //Cai dat su kien cho nut Delete
    $('.bt-delete').click(function(e){
        var user = $(this).closest('tr').find('.bt-detail').text();
        var cf = confirm("This action will delete user '" + user + "'.Are you sure?");
        if(cf === true)
        {
            $.get($(this).attr('href'), function(data, status){
               if(status == 'success')
                {
                    $(e.target).closest('tr').remove();
                    var temp = $('#message');
                    temp.html(
                    '<div class="alert alert-success">' +
                    '<button data-dismiss="alert" type="button" class="close">&times;</button>'+
                    'Your action has operated successfully!</div>');
                    temp.fadeIn(500);
                }
            });
        }
        return false;    
    });
});
//Khoi tap RichTextBox cho cac textarea co id='editor'
tinymce.init({
    selector: "#editor"
});
//Check all checkbox
function checkAll(element)
{
    var txt = $(element).text();
    if(txt == 'Check')
    {
        $('.checkbox').not(':checked').trigger('click');    
    }
    else
    {
        $('.checkbox:checked').trigger('click');
    }
    return false;
}
//Delete selected users
function deleteSelectedItems()
{
    if(checkSelected() === true)
        {
            alert('ok');
        }
}

function checkSelected()
{
    if($('.checkbox:checked').length === 0)
    {
        alert("Please select least one user");
        return false;
    }
    else
    {
        return true;
    }
}